/**
 * Created by DevilsEyes on 2016/2/13.
 */
'use strict';

var express = require('express');
var router = express.Router();

var $course = require('../midware/course');
var $session = require('../midware/session');

router.get('/list', function (req, res, next) {

    let ssid = req.cookies.ssid;
    if (ssid === undefined) {
        return res.json({
            code: 9999,
            msg: '未登录'
        });
    } else {
        var Se = $session.getBySid(ssid);
        if (Se.role >= 999) {
            $course
                .getList()
                .then((courses)=> {

                    res.json({
                        code: 0,
                        count: courses.length,
                        list: courses
                    });

                });
        } else {
            $course
                .getList(Se.class)
                .then((courses)=> {

                    res.json({
                        code: 0,
                        count: courses.length,
                        list: courses
                    });

                });
        }
    }
});

router.get('/', function (req, res, next) {

    let id = req.query._id;

    $course
        .getOnebyId(id)
        .then((course)=> {
            res.json({
                code: 0,
                info: course
            });
        })
        .catch((e)=> {
            console.log(e);
            res.json({
                code: e.code || 9999,
                msg: e.msg || '接口发生错误'
            });
        })
});

module.exports = router;